<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Favorites;

class Collect extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';

    /**
     * @var $model Favorites
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Favorites();
    }

    public function demand()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        if (!$row = \app\common\model\Demand::get($id)){
            $this->error('没有找到该需求');
        }
        $data = [
            'user_id'   => $this->auth->id,
            'collect_id'=> $id,
            'collect_name'=> 'demand'
        ];
        if ($this->model->where($data)->find()){
            $this->error('已收藏');
        }
        $data['collect_title'] = $row->title;
        $this->model->save($data);
        $this->success('收藏成功');
    }

    /**
     * 取消收藏需求
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function undemand()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        $data = [
            'user_id'   => $this->auth->id,
            'collect_id'=> $id,
            'collect_name'=> 'demand'
        ];
        if ($row = $this->model->where($data)->find()){
            $row->delete();
        }
        $this->success('取消收藏成功');
    }

    public function talent()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        if (!$row = \app\common\model\Goods::get($id)){
            $this->error('没有找到该服务');
        }
        $data = [
            'user_id'   => $this->auth->id,
            'collect_id'=> $id,
            'collect_name'=> 'goods'
        ];
        if ($this->model->where($data)->find()){
            $this->error('已收藏');
        }
        $data['collect_title'] = $row->title;
        $this->model->save($data);
        $this->success('收藏成功');
    }

    /**
     * 取消收藏服务
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function untalent()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        $data = [
            'user_id'   => $this->auth->id,
            'collect_id'=> $id,
            'collect_name'=> 'goods'
        ];
        if ($row = $this->model->where($data)->find()){
            $row->delete();
        }
        $this->success('取消收藏成功');
    }

    public function lists()
    {
        $type = input('param.type','', 'trim');
        $offset = input('param.offset',0, 'intval');
        $limit = input('param.limit',10, 'intval');
        if (!$type || !in_array($type,['demand','goods'])){
            $this->error('参数错误');
        }
        if (($offset && $offset < 0) || ($limit && $limit <= 0)){
            $this->error('参数错误');
        }
        $user = $this->auth->getUser();
        $total = $user->favorites()->where('collect_name','=', $type)->count();
        $list = $user->favorites()->with($type.'.user')->where('collect_name','=', $type)->order('create_time','desc')->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        $this->success('', $result);
    }
}